/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("cailiao_plan_data",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.DomainModels;
using ATkg.BizAppProject.IServices;
using Microsoft.AspNetCore.Authorization;
using NetTaste;
using System.Reflection;
using static ATkg.BizAppProject.Controllers.table_wgjyController;
using Newtonsoft.Json.Linq;
using DGZImp.Core.EFDbContext;
using DGZImp.Core.DbSqlSugar;
using System.Text.Json;

namespace ATkg.BizAppProject.Controllers
{
    public partial class cailiao_plan_dataController
    {
        private readonly Icailiao_plan_dataService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        public ISqlSugarClient Db = AutofacContainerModule.GetService<ServiceDbContext>()?.SqlSugarClient;

        [ActivatorUtilitiesConstructor]
        public cailiao_plan_dataController(
            Icailiao_plan_dataService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }


        [HttpGet("cailiao_plan_data_db")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> cailiao_plan_data_db()
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用");
            var r = await Service.get_db_data_cailiao_plan();
            //var r = 0;

            //var r = 0;
            return JsonNormal(r);
        }

        public class maopi_songyang_db
        {
            [SugarColumn(IsPrimaryKey = true)] // 主键标识（若存在）
            public int id { get; set; }
            public string furnaceNumber { get; set; }
            public string receiveTime { get; set; }

        }
 
        [HttpPost("maopi_songyang")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<string> maopi_songyang([FromBody] JObject jsonData)
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用,毛坯送样！！！");
            Console.WriteLine(jsonData["furnaceNumber"]);
            // 验证数据
            if (jsonData == null)
            {
                Console.WriteLine("LLL");
            }

            // 读取字段（通过字段名获取，支持容错处理）
            string furnaceNumber = jsonData.GetValue("furnaceNumber")?.ToString() ?? "";
            string receiveTimeStr = jsonData.GetValue("receiveTime")?.ToString() ?? "";

            // 验证字段有效性
            if (string.IsNullOrWhiteSpace(furnaceNumber) || !DateTime.TryParse(receiveTimeStr, out DateTime receiveTime))
            {
                Console.WriteLine("??eeeeeeee");
            }

            // 打印验证
            Console.WriteLine($"炉号：{furnaceNumber}");
            Console.WriteLine($"接收时间：{receiveTimeStr.ToString()}");            

            string result = await Service.up_to_mes_maopisongyang(furnaceNumber, receiveTimeStr);
            if(result == "True")
            {
                Console.WriteLine("成功");
                var final_data_info_fen = new List<maopi_songyang_db>();
                var data_fen = new maopi_songyang_db
                {
                    furnaceNumber = furnaceNumber,
                    receiveTime = receiveTimeStr,
                };
                final_data_info_fen.Add(data_fen);
                await Db.DoInsertAsync<maopi_songyang_db>(final_data_info_fen);
            }
            return result;
        }

        [HttpGet("maopisongyang_db")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<object> maopisongyang_db()

        {
            Console.WriteLine("get方法调用,送样获取数据库");
            var result = Db.Queryable<maopi_songyang_db>()  //                      
                    .Select(m => new
                    {
                        m.id,
                        m.furnaceNumber,
                        m.receiveTime,
                    })               // 选择 update_data 字段
                    .ToList();                               // 执行查询并返回列表
            return result;
        }

    }
}
